package br.usp.ime.components;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;

@Component
@ApplicationScoped
public class EmailSender {

	private String smtpHost;
	private int port;

	public EmailSender() {
		this.smtpHost = "smtp.ime.usp.br";
		this.port = 25;
	}

	public void sendEmail(String to, String subject, String message, String from) throws MessagingException {
		Properties props = new Properties();
		props.put("mail.smtp.host", smtpHost);
		props.put("mail.smtp.port", Integer.toString(port));
		Session session = Session.getDefaultInstance(props);
		session.setDebug(false);
		Message msg = new MimeMessage(session);
		
		InternetAddress addressTo = new InternetAddress(to);
		msg.setRecipient(Message.RecipientType.TO, addressTo);
		
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);
		
		msg.setSubject(subject);
		msg.setContent(message, "text/plain");		
		
		Transport.send(msg);
	}

	public void setPort(int port) {
		this.port = port;
	}
	
	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}
	

}
